package cn.slt.util;

import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExportUtil  
	{  
	    private XSSFWorkbook wb = null;  
	  
	    private XSSFSheet sheet = null;  
	  
	    /** 
	     * @param wb 
	     * @param sheet 
	     */  
	    public ExportUtil(XSSFWorkbook wb, XSSFSheet sheet)  
	    {  
	        this.wb = wb;  
	        this.sheet = sheet;  
	    }  
	  
	    /** 
	     * 合并单元格后给合并后的单元格加边框 
	     *  
	     * @param region 
	     * @param cs 
	     */  
	    public void setRegionStyle(CellRangeAddress region, XSSFCellStyle cs)  
	    {  
	  
	        int toprowNum = region.getFirstRow();  
	        for (int i = toprowNum; i <= region.getLastRow(); i++)  
	        {  
	            XSSFRow row = sheet.getRow(i);  
	            for (int j = region.getFirstColumn(); j <= region.getLastColumn(); j++)  
	            {  
	                XSSFCell cell = row.getCell(j);// XSSFCellUtil.getCell(row,  
	                                                // (short) j);  
	                cell.setCellStyle(cs);  
	            }  
	        }  
	    }  
	  
	    /** 
	     * 设置表头的单元格样式 
	     *  
	     * @return 
	     */  
	    public XSSFCellStyle getHeadStyle()  
	    {  
	        // 创建单元格样式  
	        XSSFCellStyle cellStyle = wb.createCellStyle();  
	        // 设置单元格的背景颜色为淡蓝色  
	        cellStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index);  
	        cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);  
	        // 设置单元格居中对齐  
	        cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);  
	        // 设置单元格垂直居中对齐  
	        cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);  
	        // 创建单元格内容显示不下时自动换行  
	        cellStyle.setWrapText(true);  
	        // 设置单元格字体样式  
	        XSSFFont font = wb.createFont();  
	        // 设置字体加粗  
	        font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);  
	        font.setFontName("宋体");  
	        font.setFontHeight((short) 200);  
	        cellStyle.setFont(font);  
	        // 设置单元格边框为细线条  
	        cellStyle.setBorderLeft(XSSFCellStyle.BORDER_NONE);  
	        cellStyle.setBorderBottom(XSSFCellStyle.BORDER_NONE);  
	        cellStyle.setBorderRight(XSSFCellStyle.BORDER_NONE);  
	        cellStyle.setBorderTop(XSSFCellStyle.BORDER_NONE);  
	        return cellStyle;  
	    }  
	  
	    /** 
	     * 设置表体的单元格样式 
	     *  
	     * @return 
	     */  
	    public XSSFCellStyle getBodyStyle()  
	    {  
	        // 创建单元格样式  
	        XSSFCellStyle cellStyle = wb.createCellStyle();  
	        // 设置单元格居中对齐  
	        cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);  
	        // 设置单元格垂直居中对齐  
	        cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);  
	        // 创建单元格内容显示不下时自动换行  
	        cellStyle.setWrapText(true);  
	        // 设置单元格字体样式  
	        XSSFFont font = wb.createFont();  
	        // 设置字体加粗  
	        font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);  
	        font.setFontName("宋体");  
	        font.setFontHeight((short) 200);  
	        cellStyle.setFont(font);  
	        // 设置单元格边框为细线条  
	        cellStyle.setBorderLeft(XSSFCellStyle.BORDER_NONE);  
	        cellStyle.setBorderBottom(XSSFCellStyle.BORDER_NONE);  
	        cellStyle.setBorderRight(XSSFCellStyle.BORDER_NONE);  
	        cellStyle.setBorderTop(XSSFCellStyle.BORDER_NONE);  
	        return cellStyle;  
	    }  
	}  


